#pragma once

#include <string>
#include <map>
#include <mutex>

#include "api/p2pms_client_interface.hpp"
#include "video_renderer_interface.hpp"

namespace p2pms
{

//==================================================================================================
// 
//==================================================================================================
class VideoRendererManager : public IRendererManager
{
public:
	VideoRendererManager(IMainThreadExecutor* executor);
	~VideoRendererManager() override = default;

	// IRendererManager
	RenderId CreateRenderer(const MediaInfo& media) override;
	bool SetRenderWindow(RenderId render_id, void* window_handle) override;
	void RefreshRender(RenderId render_id) override;
	void DestroyRenderer(RenderId render_id) override;
	void OnRenderWindowSizeChanged(RenderId render_id) override;
	void OnRenderWindowStateChanged(RenderId render_id, bool minimized) override;

	void AddVideoTrack(const MediaInfo& media, rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track);
	void RemoveVideoTrack(const MediaInfo& media);

private:
    struct MediaInfoLess 
    {
        bool operator()(const MediaInfo& a, const MediaInfo& b) const {
            return a.media_id < b.media_id;
        }
    };
	void TrySetVideoTrack(const MediaInfo& media);

private:
	std::map<MediaInfo, rtc::scoped_refptr<webrtc::VideoTrackInterface>, MediaInfoLess> m_tracks;
	std::map<RenderId, std::shared_ptr<IVideoRenderer>> m_renderers;
	IMainThreadExecutor* m_executor = nullptr;
};

} // namespace p2pms